package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/admin/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    @GetMapping("page")
    public Result pageList(SetmealPageQueryDTO setmealPageQueryDTO){
PageResult pageResult = setmealService.page(setmealPageQueryDTO);

        return Result.success(pageResult);
    }
@PostMapping
@CacheEvict(cacheNames = "setmealCache",key = "#setmealVO.categoryId")
    public Result addSermealOrDish(@RequestBody SetmealVO setmealVO){
setmealService.addSermealOrDish(setmealVO);
return Result.success();
}
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result openOrClose(@PathVariable Integer status, Long id){
        setmealService.openOrClose(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result byId(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.byId(id);

        return Result.success(setmealVO);
    }

@PutMapping
@CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result updateSetmealDish(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetmealDish(setmealDTO);
return  Result.success();

    }

    @DeleteMapping
    @CacheEvict(cacheNames ="setmealCache",allEntries = true)
    public Result deleteSetmeal(Integer[] ids){
setmealService.deleteSetmeal(ids);
        return  Result.success();
    }

}
